package com.controller;

import com.pojo.Provider;
import com.pojo.User;
import com.service.UserService;
import com.utils.Md5Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;
import java.util.Objects;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private UserService us = new UserService();
    private Integer currentPageNo = 1;
    private Integer pageSize = 8;

    public void userlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<User> users = us.selectByPage(0, pageSize);
        List<User> users = us.queryUserAll();
        req.getSession().setAttribute("userList", users);
//        req.getSession().getAttribute("userList");

//        req.getSession().setAttribute("totalCount",users.size());
//        req.getSession().setAttribute("currentPageNo",1);

        List<User> listThis = us.getUsers(currentPageNo, pageSize);
        req.getSession().setAttribute("SERVLET_NOW","user");
        req.getSession().setAttribute("userList", listThis);
        req.getSession().setAttribute("currentPageNo", currentPageNo);
        req.getSession().setAttribute("totalCount", users.size());
        if (users.size()!=0){
            req.getSession().setAttribute("totalPageCount", ((users.size()-1)/ pageSize)+1);
        }else {
            req.getSession().setAttribute("totalPageCount", 0 );
        }

        resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
    }


    public void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        int userCode = Integer.parseInt(req.getParameter("userCode"));
        String userName = req.getParameter("userName");
        String userPassword = req.getParameter("userPassword");
        String ruserPassword = req.getParameter("ruserPassword");
        Boolean gender = Boolean.parseBoolean(req.getParameter("gender"));
        Date birthday = Date.valueOf(req.getParameter("birthday"));
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String userRoleName = req.getParameter("userRoleName");

        if (us.exit(userName)) {
            req.getSession().setAttribute("EXIT_USER", "用户名已存在");
//            resp.sendRedirect(req.getContextPath() + "/jsp/useradd.jsp");
            req.getRequestDispatcher("/jsp/useradd.jsp").forward(req, resp);
        } else {
            User u = new User(userCode, userName, gender, userPassword, ruserPassword, phone, address, userRoleName, birthday);

            boolean b = us.addUser(u.getUserCode(), u.getUserName(), u.getRuserPassword(), u.getRuserPassword(), u.isGender(), u.getBirthday(), u.getPhone(), u.getAddress(), u.getUserRoleName());
            if (b) {
                resp.sendRedirect(req.getContextPath() + "/user/userlist");
            } else {
                resp.sendRedirect(req.getContextPath() + "/jsp/useradd.jsp");
            }
        }
    }

    public void edituser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User user = us.queryById(id);

        req.getSession().setAttribute("USER", user);
        Object user1 = req.getSession().getAttribute("USER");
        req.getSession().setAttribute("id", user.getId());
        resp.sendRedirect(req.getContextPath() + "/jsp/usermodify.jsp");
    }

    public void edituser1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = String.valueOf(req.getSession().getAttribute("id"));
        String userName = req.getParameter("userName");
        Boolean gender = Boolean.parseBoolean(req.getParameter("gender"));
        Date birthday = Date.valueOf(req.getParameter("birthday"));
        String phone = req.getParameter("phone");
        String userRoleName = req.getParameter("userRoleName");

        boolean b = us.updateUser(id, userName, gender, birthday, phone, userRoleName);
        resp.sendRedirect(req.getContextPath() + "/user/userlist");
    }

    public void viewuser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User user = us.queryById(id);
        req.getSession().setAttribute("user", user);
        req.setAttribute("PWD_MSG", "两次密码不一致");

        resp.sendRedirect(req.getContextPath() + "/jsp/userview.jsp");
    }

    public void deleteuser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        System.out.println(id);
        us.deleteById(id);
        resp.sendRedirect(req.getContextPath() + "/user/userlist");

    }

    public void reasch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryname = req.getParameter("queryname");
        String queryRoleName = req.getParameter("queryRoleName");
        UserService us = new UserService();
        List<User> users = us.queryByName(queryname, queryRoleName);
        req.getSession().setAttribute("userList", users);

        req.getSession().setAttribute("queryUserName", queryname);
        req.getSession().setAttribute("queryuserRoleName", queryRoleName);

        resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");

    }

    public void pwdModify(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        UserService service = new UserService();
        String oldpassword = req.getParameter("oldpassword");
//        String username = (String) req.getSession().getAttribute("LOGIN_NAME");
        String username = "xiaowan";
        User user = service.getUser(username);
        oldpassword = Md5Utils.MD5Encode(oldpassword, "utf-8", false);
        if (!user.getUserPassword().equals(oldpassword)) {
            req.getSession().setAttribute("PWD_MSG", "密码输入有误");
//            resp.sendRedirect(req.getContextPath() + "/jsp/pwdmodify.jsp");
            req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);
        } else {
            String newpassword = req.getParameter("newpassword");
            String rnewpassword = req.getParameter("rnewpassword");
            if (!Objects.equals(newpassword, rnewpassword)) {
                req.getSession().setAttribute("PWD_MSG", "两次密码不一致");
//                resp.sendRedirect(req.getContextPath() + "/jsp/pwdmodify.jsp");
                req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);
            } else {
                newpassword = Md5Utils.MD5Encode(newpassword, "utf-8", false);
                service.pwdModify(username, newpassword);
                req.getSession().setAttribute("PWD_MSG", "修改成功");
//        req.getRequestDispatcher("pwdModefyMsg").forward(req,resp);
//                resp.sendRedirect(req.getContextPath() + "/jsp/pwdmodify.jsp");
                req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);
            }
        }
    }

    public void pwdMsgClear(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        req.getSession().setAttribute("PWD_MSG",null);
//        resp.sendRedirect(req.getContextPath()+"/jsp/pwdmodify.jsp");
        req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);
    }

    public void pageFirst(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        currentPageNo = 1;
        resp.sendRedirect("userlist");
    }

    public void pageBefore(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        if (this.currentPageNo>1){
            this.currentPageNo -= 1;
        }else {
            currentPageNo = 1;
        }
        resp.sendRedirect("userlist");
    }

    public void pageNext(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer totalPageCount = (Integer) req.getSession().getAttribute("totalPageCount");
        if (totalPageCount > currentPageNo){
            currentPageNo += 1;
        }else {
            currentPageNo = totalPageCount;
        }
        resp.sendRedirect("userlist");
    }

    public void pageFinal(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        currentPageNo = (Integer) req.getSession().getAttribute("totalPageCount");
        resp.sendRedirect("userlist");
    }


//    public void firstPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//        List<User> users = us.selectByPage(0, pageSize);
//        List<User> user = us.queryUserAll();
//        req.getSession().setAttribute("totalCount",user.size());
//        req.getSession().setAttribute("currentPageNo",1);
//
//        req.getSession().setAttribute("userList",users);
//        resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//    }
//    //0 1 2 3 4
//    //pageSize 6 7 8 9
//    //10 11 12 13 14
//    //分页计算公式 start:就是当前页的起始索引,
//    // pageSize:就是每页的条数
//    // count_data:总数据条数
//    // currentPage:就是当前页
//    // countPage:总页数
//    // 当前页的起始索引公式: start=(currentPage-1)*pageSize
//    // 总页数的公式： countPage=(count_data+pageSize-1)//pageSize
//    static int currentpage = 1;
////    static int totalPages = 0;
//    public void nextPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<User> users = us.queryUserAll();
//        int countPage = users.size();
//        req.getSession().setAttribute("totalCount",users.size());
//        double count = countPage/pageSize;
//        if(currentpage>Math.ceil(count)){
//            currentpage--;
////            totalPages = (countPage+currentpage*pageSize-1)/pageSize;
//            List<User> users1 = us.selectByPage(currentpage*pageSize,pageSize);
//            req.getSession().setAttribute("currentPageNo",currentpage+1);
//            req.getSession().setAttribute("userList", users1);
//            resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//            currentpage++;
//            return;
//        }else{
//
//            List<User> users1 = us.selectByPage(currentpage*pageSize,pageSize);
////            totalPages = (countPage+currentpage*pageSize-1)/pageSize;
//            req.getSession().setAttribute("userList", users1);
//            req.getSession().setAttribute("currentPageNo",currentpage+1);
//            resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//            currentpage++;
//            return;
//        }
//    }
//
//    public void upPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        currentpage--;
//        List<User> users = us.queryUserAll();
//        int countPage = users.size();
//        req.getSession().setAttribute("totalCount",users.size());
//        if(currentpage<0){
//            currentpage++;
//        List<User> users1 = us.selectByPage((currentpage*pageSize),pageSize);
//        req.getSession().setAttribute("userList", users1);
//        req.getSession().setAttribute("currentPageNo",currentpage+1);
//        resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//            return;
//        }else{
//            List<User> users1 = us.selectByPage((currentpage*pageSize),pageSize);
//            req.getSession().setAttribute("userList", users1);
//            req.getSession().setAttribute("currentPageNo",currentpage+1);
//            resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//            return;
//        }
//    }
//
//    public void lastPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        List<User> users = us.queryUserAll();
//        int count = users.size()/pageSize;
//        currentpage = (int) Math.floor(count);
//        req.getSession().setAttribute("totalCount",users.size());
//        if(currentpage>Math.ceil(count)){
//            currentpage--;
//            List<User> users1 = us.selectByPage((currentpage * pageSize), pageSize);
//            req.getSession().setAttribute("currentPageNo",currentpage+1);
//            req.getSession().setAttribute("userList", users1);
//            resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//        }else {
//            List<User> users1 = us.selectByPage((currentpage * pageSize), pageSize);
//            req.getSession().setAttribute("currentPageNo",currentpage+1);
//            req.getSession().setAttribute("userList", users1);
//            resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//        }
//    }
//
//    public void jumpTo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int inputPage = Integer.parseInt(req.getParameter("inputPage"));
//
//            req.getSession().setAttribute("currentPageNo",inputPage);
//            req.getSession().setAttribute("jump",inputPage);
//            inputPage--;
//            List<User> users1 = us.selectByPage((inputPage * pageSize), pageSize);
//            req.getSession().setAttribute("userList", users1);
//            resp.sendRedirect(req.getContextPath() + "/jsp/userlist.jsp");
//            return;
//
//
//    }
}
